﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Toenda.Foundation.IO;
using Toenda.Foundation.Data;
using Toenda.Foundation.Types;

namespace Toenda.Foundation.Messaging {
	/// <summary>
	/// Provides a reader for the xml file with the sql strings for the internal messages.
	/// </summary>
	public class SqlResources {
		/// <summary>
		/// Gets the <see cref="Toenda.Foundation.Messaging.SqlResources"/> at the specified index.
		/// </summary>
		/// <value></value>
		public string this[string index] {
			get {
				if(File.Exists(@"Data\MessagingSqlResources.xml")) {
					XmlProvider xml = new XmlProvider(@"Data\MessagingSqlResources.xml");

					if(xml.IsValidXml) {
						List<XmlNodeTemplate> list = xml.ReadTagListWithAttributes(
							"root",
							true
						);

						Dictionary<string, string> resources = new Dictionary<string, string>();

						foreach(XmlNodeTemplate part in list) {
							string value = part.Value;
							string key = "";

							foreach(XmlAttributeTemplate attr in part.Attributes) {
								if(attr.Name == "name") {
									key = attr.Value;
									break;
								}
							}

							resources.Add(key, value);
						}

						return resources[index];
					}
				}

				return "";
			}
		}
	}
}
